﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;using System.Configuration;
using System.Windows.Forms;

namespace ChairtyBank.DAL
{
    class AkhesEntity
    {
    
        public String name { get; set; }


    }

    /*
 
        Class Client which contains all methods related to Customer table
 
    */


    class Akhes
    {
        //Set absolute DB path rather than relative to solve openFileDialog reset path problem
        private static string conString = ConfigurationManager.ConnectionStrings["ChairtyBank"].ToString();
            //ConfigurationManager.ConnectionStrings["ChairtyBank"].ToString();
        private static string cmdText = "";





        public static List<AkhesEntity> GetAll()
        {
            List<AkhesEntity>  IList = new List<AkhesEntity>();


            cmdText = "select * from Akhesaye ";
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);
            
            try
            {
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();
                while(reader.Read())
                {

                    IList.Add(fillEntityFromReader(reader));
                }
        
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception==>>" + ex.Message);
            }

            finally
            {
                if(con.State == ConnectionState.Open)
                    con.Close();
            }

            return IList;
        }


     




        public static void Add(String AkhesName)
        {
            cmdText = "insert into Akhesaye (Name)values(@Instalment_DueDate)";
              
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@Instalment_DueDate", AkhesName);
           
            try
            {
                con.Open();

                Int32 numOfRow = cmd.ExecuteNonQuery();
              
               
            }
            catch(Exception ex){
                MessageBox.Show("Exception==>>"+ex.Message);
            }
            finally
            {
                if(con.State == ConnectionState.Open)
                    con.Close();
            }

            

        }






        public static bool Delete(String I)
        {
            Boolean flag = false;
            cmdText = "Delete from Akhesaye WHERE Name=@name";

                SqlConnection con = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand(cmdText, con);
                cmd.Parameters.AddWithValue("@name", I);
                try
                {
                    con.Open();

                    Int32 numOfRow = cmd.ExecuteNonQuery();
                    flag = true;


                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception==>>" + ex.Message);
                }
                finally
                {
                    if (con.State == ConnectionState.Open)
                        con.Close();
                }
            
            return flag;
        }





   




        private static AkhesEntity fillEntityFromReader(SqlDataReader reader)
        {
         

            AkhesEntity I = new AkhesEntity();

            I.name = reader["Name"].ToString();
            return I;
        }

         
    }
}
